package com.mzkj.wanjulaile.mobile.api.modules.activeConfig.controller;


import com.mzkj.common.data.StatusCode;
import com.mzkj.common.utils.Collections3;
import com.mzkj.common.utils.StringUtils;
import com.mzkj.wanjulaile.mobile.api.common.controller.BaseController;
import com.mzkj.wanjulaile.mobile.api.common.entity.ResultEntity;
import com.mzkj.wanjulaile.mobile.api.modules.account.entity.Account;
import com.mzkj.wanjulaile.mobile.api.modules.account.entity.BuyVip;
import com.mzkj.wanjulaile.mobile.api.modules.account.service.AccountService;
import com.mzkj.wanjulaile.mobile.api.modules.account.service.BuyVipService;
import com.mzkj.wanjulaile.mobile.api.modules.activeConfig.entity.BannerConfig;
import com.mzkj.wanjulaile.mobile.api.modules.activeConfig.service.BannerConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Random;

/**
 * 分享活动配置Controller
 *
 * @author ianly
 * @version 2017-02-08
 */
@Controller
@RequestMapping(value = "activeConfig")
public class ActiveShareConfigController extends BaseController {

    @Autowired
    private BannerConfigService bannerConfigService;
    @Autowired
    private AccountService accountService;
    @Autowired
    private BuyVipService buyVipService;

    /**
     * banner图片
     * 传递platform参数 逗号分隔(1,2,3) 1公众号 2Android 3iOS
     */
    @ResponseBody
    @RequestMapping("banner")
    public ResultEntity<List<BannerConfig>> getBanner(BannerConfig bannerConfig, String accessToken){
        ResultEntity<List<BannerConfig>> result = new ResultEntity<>();
        try {
            if (StringUtils.isNotEmpty(accessToken)){
                //登陆状态
                //查询账号类型
                Account account = accountService.getAccountByAccessToken(accessToken);
                //是否购买过
                boolean buyFlag = false;
                List<BuyVip> allList = buyVipService.findListByAccountId(account.getId());
                if (!Collections3.isEmpty(allList)){
                    buyFlag = true;
                }
                if (account.getVipType() == 0 && buyFlag){
                    //过期用户
                    bannerConfig.setType("2");
                }
                if (account.getVipType() == 0 && !buyFlag){
                    //新用户
                    bannerConfig.setType("0");
                }
                if (account.getVipType() != 0){
                    //会员
                    bannerConfig.setType("1");
                }
            }else {
                bannerConfig.setType("0");
            }
            bannerConfig.setShowFlag("0");
            List<BannerConfig> bannerConfigList = bannerConfigService.findList(bannerConfig);
            if (bannerConfigList.size()>0){
                result.setData(bannerConfigList);
                result.setStatusCode(StatusCode.SC0.getValue());
                result.setMsg(StatusCode.SC0.getDesc());
            }else {
                result.setStatusCode(StatusCode.SC4002.getValue());
            }
        } catch (Exception e) {
            e.printStackTrace();
            result.setStatusCode(StatusCode.SC5000.getValue());
            result.setMsg(StatusCode.SC5000.getDesc());
        }
        return result;
    }

}